﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PizzaLunch
{
    public class JobList : List<QueryJob>
    {
        public void Push(QueryJob job)
        {
            lock (this)
            {
                if (!Contains(job))
                {
                    Add(job);
                }
            }
            MainControl.queryEvent.Set();
        }
        public void PushHead(QueryJob job)
        {
            lock (this)
            {
                if (Contains(job))
                {
                    Remove(job);
                }
                Insert(0, job);
            }
            MainControl.queryEvent.Set();
        }
        public QueryJob Pop()
        {
            lock (this)
            {
                if (Count > 0)
                {
                    QueryJob job = this[0];
                    RemoveAt(0);
                    return job;
                }
            }
            return null;
        }
    }

    public abstract class QueryJob
    {
        public abstract void Working();
    }

    public class QueryTimelineJob : QueryJob
    {
        public TweetAccount Account { get; set; }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override bool Equals(System.Object obj)
        {
            if (obj == null)
                return false;
            if (obj.GetType()!=GetType())
                return false;

            QueryTimelineJob job = obj as QueryTimelineJob;
            if ((System.Object)job == null)
                return false;

            if (job.Account.Equals(Account))
                return true;
            else
                return false;
        }
        public override void Working()
        {
            MainControl.Logger.WriteLog("Query job timeline", LoggerFile.LoggerLevel.LoggerDetail);
            Account.Query();
        }
    }

    public class QueryCommentsJob : QueryJob
    {
        public TweetItem Item { get; set; }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override bool Equals(System.Object obj)
        {
            if (obj == null)
                return false;
            if (obj.GetType()!=GetType())
                return false;

            QueryCommentsJob job = obj as QueryCommentsJob;
            if ((System.Object)job == null)
                return false;
            if (job.Item.Equals(Item))
                return true;
            else
                return false;
        }
        public override void Working()
        {
            MainControl.Logger.WriteLog("Query job comments", LoggerFile.LoggerLevel.LoggerDetail);
            Item.GetComments();
        }
    }

    public class QueryFansJob : QueryJob
    {
        public TweetAccount Account { get; set; }
        public UserInfo User { get; set; }
        public int Index { get; set; }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override bool Equals(System.Object obj)
        {
            if (obj == null)
                return false;
            if (obj.GetType()!=GetType())
                return false;

            QueryFansJob job = obj as QueryFansJob;
            if ((System.Object)job == null)
                return false;
            if (job.Account.Equals(Account) && job.User.Equals(User))
                return true;
            else
                return false;
        }
        public override void Working()
        {
            MainControl.Logger.WriteLog("Query job comments", LoggerFile.LoggerLevel.LoggerDetail);
            Account.GetFansList(User, UserInfo.UserType.USER_FANS, Index);
        }
    }

    public class QueryFriendsJob : QueryJob
    {
        public TweetAccount Account { get; set; }
        public UserInfo User { get; set; }
        public int Index { get; set; }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override bool Equals(System.Object obj)
        {
            if (obj == null)
                return false;
            if (obj.GetType()!=GetType())
                return false;

            QueryFriendsJob job = obj as QueryFriendsJob;
            if ((System.Object)job == null)
                return false;
            if (job.Account.Equals(Account) && job.User.Equals(User))
                return true;
            else
                return false;
        }
        public override void Working()
        {
            MainControl.Logger.WriteLog("Query job comments", LoggerFile.LoggerLevel.LoggerDetail);
            Account.GetFansList(User, UserInfo.UserType.USER_FRIENDS, Index);
        }
    }

    public class QueryUserTimelineJob : QueryJob
    {
        public TweetAccount Account { get; set; }
        public UserInfo User { get; set; }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override bool Equals(System.Object obj)
        {
            if (obj == null)
                return false;
            if (obj.GetType() != GetType())
                return false;

            QueryUserTimelineJob job = obj as QueryUserTimelineJob;
            if ((System.Object)job == null)
                return false;

            if (job.Account.Equals(Account) && job.User.Equals(User))
                return true;
            else
                return false;
        }
        public override void Working()
        {
            MainControl.Logger.WriteLog("Query job timeline", LoggerFile.LoggerLevel.LoggerDetail);
            Account.QueryUserTimeline(User);
        }
    }

}
